<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

<!--    卡片-->
    <el-card>
      <el-alert
        title="注意：只允许为第三级分类设置相关参数"
        type="warning" :closable="false" show-icon>
      </el-alert>

<!--      选择商品分类-->
      <el-row class="rowx">
        <el-col>
          <span>选择商品分类: </span>
          <el-cascader
            v-model="selectcatekeys"
            :options="catelist"
            expandTrigger="hover"
            :props="cateprops"
            @change="handleChange"></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handletabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button @click="adddialogVisible = true" type="primary" size="mini" :disabled="isbtndis">添加参数</el-button>
          <el-table class="mt" :data="manytabledata" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="'参数名称" prop="attr_name"></el-table-column>
            <el-table-column>
              <el-table-column label="操作">
                <template>
                  <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                  <el-button sizi="mini" type="danger" icon="el-icon-delete">删除</el-button>
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="onli">
          <el-button @click="adddialogVisible = true" type="primary" size="mini" :disabled="isbtndis">添加属性</el-button>
          <el-table class="mt" :data="onlytabledata" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="'属性名称" prop="attr_name"></el-table-column>
            <el-table-column>
              <el-table-column label="操作">
                <template>
                  <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                  <el-button sizi="mini" type="danger" icon="el-icon-delete">删除</el-button>
                </template>
              </el-table-column>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog
      :title="'添加' + titletext"
      :visible.sync="adddialogVisible"
      width="50%">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="adddialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="adddialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Params',
  data () {
    return {
      catelist: [],
      cateprops: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectcatekeys: [],
      activeName: 'many',
      manytabledata: [],
      onlytabledata: [],
      adddialogVisible: false
    }
  },
  created() {
    this.getcatelist()
  },
  methods: {
    async getcatelist () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      this.catelist = res.data
    },
    async handleChange () {
      this.getparmsdata()
    },
    handletabClick () {
      this.getparmsdata()
    },
    async getparmsdata () {
      if (this.selectcatekeys !== 3) {
        this.selectcatekeys = []
        return
      }
      const { data: res } = await this.$http.get(`categories/${this.cateid}/attributes`, { params: { sel: this.activeName } })
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }
      console.log(res.data)
      if (this.activeName === 'many') {
        this.manytabledata = res.data
      } else {
        this.onlytabledata = res.data
      }
    }
  },
  computed: {
    isbtndis () {
      if (this.selectcatekeys.length !== 3) {
        return true
      }
      return false
    },
    cateid () {
      if (this.selectcatekeys.length === 3) {
        return this.selectcatekeys[2]
      }
      return null
    },
    titletext () {
      if (this.activeName === 'many') {
        return '动态参描'
      }
      return '静态属性'
    }
  }
}
</script>

<style scoped>
 .rowx{
   margin: 20px 0;
 }
 .mt{
   margin-top: 15px;
 }
</style>
